﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IceCS.Img2Bytes
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件(*.jpg,*.png,*.ico)|*.jpg;*.png;*.ico";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = Path.Combine(Environment.CurrentDirectory, "test-files");
            var showResult = openFileDialog1.ShowDialog();
            if (showResult == DialogResult.Cancel) { return; }
            var fileStream = openFileDialog1.OpenFile();
            var ms1 = new MemoryStream();
            var ms2 = new MemoryStream();
            fileStream.CopyTo(ms1);
            fileStream.Position = 0;
            fileStream.CopyTo(ms2);
            ms1.Position = 0;
            ms2.Position = 0;
            var text = Core.ConvertImgToBytesTextHex(ms2);
            textBox1.Text = text;
            ms1.Seek(0, SeekOrigin.Begin);
            pictureBox1.Image = new Bitmap(ms1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var text = textBox1.Text;
            var stream = Core.ConvertBytesTextHexToImg(text);
            try
            {
                pictureBox1.Image = new Bitmap(stream);
            }
            catch
            {
                pictureBox1.Image = null;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
            textBox1.Copy();
            MessageBox.Show("复制成功");
        }
    }
}
